﻿#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>

using namespace std;
using namespace cv;


int main() {
	Mat image, image_gray, image_blur, image_threshold, image_bw1, image_bw2 ,image_bw3, image_bw4;

	//读取图片
	image = imread("D:/opencv/Project/Opencv_Project/2.png");
	if (image.empty())
	{
		cout << "读取图像出错" << endl;
		return -1;
	}
	imshow("image", image);

	//灰度处理
	cvtColor(image, image_gray, COLOR_BGR2GRAY);

	//均值滤波
	blur(image_gray, image_blur, Size(5, 5));
	

	//阀值分割
	threshold(image_blur, image_threshold,118,255, THRESH_BINARY_INV);

	//腐蚀膨胀
	//核为矩形5x5腐蚀
	Mat se = getStructuringElement(2, Size(5, 5)); //构造矩形结构元素
	erode(image_threshold, image_bw1, se, Point(-1, -1), 1); //执行腐蚀操作

	//核为圆形9x9开运算
	Mat se2 = getStructuringElement(2, Size(9, 9)); //构造矩形结构元素
	morphologyEx(image_bw1, image_bw2, MORPH_OPEN,se2, Point(-1, -1), 1); 

	//9x9椭圆核开闭运算
	morphologyEx(image_bw2, image_bw3, MORPH_OPEN, se2, Point(-1, -1), 1);
	morphologyEx(image_bw3, image_bw4, MORPH_CLOSE, se2, Point(-1, -1), 1);
	
	/*vector<vector<Point>> contours;
	vector<Vec4i> hierarchy;
	findContours(image_bw4, contours, RETR_LIST, CHAIN_APPROX_NONE);


	//绘制轮廓,内填充
	for (int i = 0; i < contours.size(); i++) {
		
	}

	drawContours(image_bw4, image);*/
	imshow("image_bw4", image_bw4);

	waitKey();

	return 0;
}

